package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class DangCongSanNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://cn.dangcongsan.vn/[a-z\\-]+/.*-[0-9]+.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title =doc.select("h1").text();
		date=DateUtils.matchDate(doc.select("span.post-subinfo").text());
		content =HtmlCleaner.getContentHtml(url, doc.select("div.row"));
		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	public static void main(String[] args) {
		DangCongSanNewsAnalyse analyse=new DangCongSanNewsAnalyse();
		String url="http://cn.dangcongsan.vn/culture-sports/%E2%80%9C%E5%96%9C%E8%BF%8E2016%E6%96%B0%E5%B9%B4%EF%BC%9A%E9%A5%AE%E9%A3%9F%E2%80%94%E8%B6%8A%E5%8D%97%E6%B0%91%E6%97%8F%E6%96%87%E5%8C%96%E5%85%83%E7%B4%A0%E2%80%9D%E6%B4%BB%E5%8A%A8%E5%8D%B3%E5%B0%86%E5%9C%A8%E6%B2%B3%E5%86%85%E4%B8%BE%E8%A1%8C-364108.html";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}

